﻿using B.S.BasicData.Api.Write.Application.Command;
using B.S.BasicData.Api.Write.Application.Command.Role;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace B.S.BasicData.Api.Write.Controllers
{
    /// <summary>
    /// 角色控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class RoleController : ControllerBase
    {
        private readonly IMediator mediator;

        public RoleController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> AddRole(AddRoleCommand request)
        {
            var userid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            var userName = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
            var roleName = HttpContext.User.FindFirst(ClaimTypes.Role)?.Value;
            var customerValue = HttpContext.User.FindFirst("CustomClaimType")?.Value;
            var key = HttpContext.User.FindFirst("Key")?.Value;
            return mediator.Send(request);
        }
        /// <summary>
        /// 角色修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public Task<ApiResult<int>> UpdateRole(UpdateRoleCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public Task<ApiResult<int>> DelRole(DelRoleCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 分配角色
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> AddUserRole(AddUserRoleCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 删除角色中的用户
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> DelUserRole(DelUserRoleCommand request)
        {
            return mediator.Send(request);
        }
    }
}
